package com.szxy.stream;

import com.szxy.bean.Employee;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Author:zwer
 * @Date:2020/4/16 12:14
 * @Description:com.szxy.stream
 * @Version:1.0
 *
 **/
public class TestMiddleStream {


    @Test
    public void test1() {
        List<Employee> list =  new ArrayList<>();
        list.add(new Employee(1, "张三", 10000));
        list.add(new Employee(2, "李四", 9000));
        list.add(new Employee(1, "张三", 10000));
        list.add(new Employee(4, "王五", 2000));
        // 创建 Stream 流对象
        Stream<Employee> stream = list.stream();

        // 中间操作
        // stream =  stream.filter(emp -> !emp.getName().equals("张三"));
        // distinct 方法依赖于 equals 方法判断
        // stream = stream.distinct();
        // 终结操作
        // stream.forEach(System.out::println);
        stream.filter(emp -> emp.getSalary() >= 9000)
                .distinct()
                .forEach(System.out::println);

    }

    @Test
    public void test2() {
        long count = Stream.of(1, 2, 3, 2, 3, 4, 6, 9, 22, 21)
                .limit(6)
                .distinct()
                .peek(System.out::println)
                .count();
        System.out.println("count = " + count);
    }

    @Test
    public void test4() {
        Stream.of(1, 2, 3, 0).map(elm -> elm*100).forEach(System.out::println);
    }

    @Test
    public void test3() {
        long count = Stream.of("Hello|World|Java", "Java|Hello|Python")
                .flatMap(elm -> Stream.of(elm.split("\\|")))
                .peek(System.out::println)
                .count();
        System.out.println("count = " + count);
    }
}
